package com.mycat.manager.controller.common;

import com.mycat.dao.dto.auth.MenuResourcesDTO;
import com.mycat.manager.common.BaseController;
import com.mycat.service.auth.AuthResourceService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author roger.
 * @email luojie@yzworld.cn
 * @date 2016/10/8
 */
@Controller
@RequestMapping
public class IndexController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    public final static String SESSION_INDEX_MENU = "indexMenu";

    private static final Integer parentId = 0;

    @Autowired
    private AuthResourceService authResourceService;

    /**
     * 进入用户管理
     */
    @RequestMapping("/index")
    public String toList() {
        try {
            Subject subject = SecurityUtils.getSubject();
            /* rememberMe后session数据会丢失。需要再次恢复session中的数据 */
            if(subject.isRemembered() || subject.isAuthenticated()){
                List<Integer> roleIds = super.getLoginRoleIds();
                List<MenuResourcesDTO> resources = authResourceService.findPageResourcesByRoleIds(parentId, roleIds.toArray(new Integer[]{}));
                Session session = SecurityUtils.getSubject().getSession();

                session.setAttribute("user",super.getLoginUser());
                session.setAttribute(SESSION_INDEX_MENU,resources);
            }
            return "common/index";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}
